package vistas.paneles;

import java.awt.Dimension;
import java.awt.ScrollPane;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JPanel;

import vistas.abstractModels.SelectionListModel;

public class JSelectionList extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JList<Object> nonSelected, selected;
	private DefaultListModel lMnonSelected, lMSelected;
	private ScrollPane scrollNon, scrollSelected;
	private JButton remove, addItem;
	private JPanel buttons;
	

	@SuppressWarnings("unchecked")
	public <T> JSelectionList(ArrayList<T> all, ArrayList<T> sel){
		
		System.out.println(sel.size());
		
		//nonselected = all - selected
		//all.removeAll(sel);
		lMnonSelected = new DefaultListModel<T>();
		Iterator<T> it = all.iterator();
		while(it.hasNext()){
			lMnonSelected.addElement(it.next());
		}
		
		lMSelected = new DefaultListModel<T>();
		
		it = sel.iterator();
		while(it.hasNext()){
			T t = it.next();
			// PARCHE
			if(!lMSelected.contains(t))
			//PARCHE
				lMSelected.addElement(t);
		}
		
		nonSelected = new JList<Object>(lMnonSelected);
		//nonSelected.setPreferredSize(new Dimension(150, 250));
		
		
		selected = new JList<Object>(lMSelected);
		//selected.setPreferredSize(new Dimension(150, 250));
		
		scrollNon = new ScrollPane();
		scrollNon.setPreferredSize(new Dimension(150, 250));
		
		scrollSelected = new ScrollPane();
		scrollSelected.setPreferredSize(new Dimension(150, 250));
		
		remove = new JButton("Quitar");
		addItem = new JButton("Agregar");
		
		buttons = new JPanel();
		buttons.setLayout(new BoxLayout(buttons, BoxLayout.Y_AXIS));
		buttons.setPreferredSize(new Dimension(100, 100));
		
		agregar_componentes();
		
		setPreferredSize(new Dimension(450,300));
		
		listeners();
		
	}
	
	private void agregar_componentes() {
	
		buttons.add(addItem);
		buttons.add(remove);
		
		scrollNon.add(nonSelected);
		
		scrollSelected.add(selected);

		add(scrollNon);
		add(buttons);
		add(scrollSelected);

	}

	private void listeners() {
		addItem.addActionListener(new ActionListener() {
		
			@Override
			public void actionPerformed(ActionEvent e) {
				if(nonSelected.getSelectedIndex() < 0)
					return;
				for(int i = 0; i < lMSelected.getSize(); i++){
					if(lMSelected.getElementAt(i).toString().equals(lMnonSelected.getElementAt(nonSelected.getSelectedIndex()).toString()))
						return;
				}
				lMSelected.addElement(lMnonSelected.getElementAt(nonSelected.getSelectedIndex()));
//				//selected.setModel(lMSelected);
//
//				selected.validate();
//				selected.repaint();
//				
//				scrollSelected.validate();
//				scrollSelected.repaint();
//				
//	
				
//				validate();
//				repaint();//				validate();
//				repaint();

				
			}
		});
		
		remove.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				if(selected.getSelectedIndex() < 0)
					return;
				lMSelected.removeElement(lMSelected.getElementAt(selected.getSelectedIndex()));
//			//	selected.setModel(lMSelected);
//				
//				selected.validate();
//				selected.repaint();
//				
//				scrollSelected.validate();
//				scrollSelected.repaint();
//				
//				
//				validate();
//				repaint();
			}
		});
		
	}

	@SuppressWarnings("unchecked")
	public <T> List<T> getChosen() {
		ArrayList <T>al = new ArrayList<T>();
		for(int i = 0; i < lMSelected.getSize(); i++){
			al.add((T) lMSelected.getElementAt(i));
			System.out.println(lMSelected.getElementAt(i));
		}
		return al;
	}
	
	
	
}
